#include "StdAfx.h"
#include "BlackScholesMertonHWProcess.h"

#include <ql/termstructures/volatility/equityfx/localvolsurface.hpp>
#include <ql/termstructures/volatility/equityfx/localvolcurve.hpp>
#include <ql/termstructures/volatility/equityfx/localconstantvol.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>

namespace QuantLib 
{

    BlackScholesMertonHWProcess::BlackScholesMertonHWProcess(
                              const Handle<Quote>& x0,
                              const Handle<YieldTermStructure>& dividendTS,
                              const Handle<YieldTermStructure>& riskFreeTS,
                              const Handle<BlackVolTermStructure>& blackVolTS,
							  const Handle<YieldTermStructure>& employeeLossTS,
                              const boost::shared_ptr<discretization>& d)
    : BlackScholesMertonProcess(x0,dividendTS,riskFreeTS,blackVolTS,d), 
	  employeeLossRate_(employeeLossTS)
	{
	}

	Real BlackScholesMertonHWProcess::drift(Time t, Real x) const
	{
		Real retVal = BlackScholesMertonProcess::drift(t, x);
		// we could be more anticipatory if we know the right dt
        // for which the drift will be used
        Time t1 = t + 0.0001;
		Real instantEmpLoss = employeeLossRate()->forwardRate(t, t1, Continuous, NoFrequency, true);
		return retVal - instantEmpLoss;
	}

	Real BlackScholesMertonHWProcess::evolve(Time t0, Real x0,
											 Time dt, Real dw) const
	{
		Real retVal = BlackScholesMertonProcess::evolve(t0, x0, dt, dw);
		Real instantEmpLoss = employeeLossRate()->forwardRate(t0, t0 + dt, Continuous, NoFrequency, true);
						
		retVal *= std::exp(-instantEmpLoss * dt);
		return retVal;
	}	
}